package com.mx.controller;

import com.mx.mapper.ArticleMapper;
import com.mx.mapper.AttentionMapper;
import com.mx.mapper.StarMapper;
import com.mx.mapper.UserMapper;
import com.mx.pojo.Article;
import com.mx.pojo.Attention;
import com.mx.pojo.Star;
import com.mx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class JsonController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    StarMapper starMapper;

    @Autowired
    AttentionMapper attentionMapper;

    @Autowired
    ArticleMapper articleMapper;

    @RequestMapping("/forUsername")
    public boolean forUsername(String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username",username);
        List<User> users = userMapper.selectByMap(map);
        if(users==null||users.size()==0)
        {
            return false;
        }
        return true;
    }
    @RequestMapping("/star")
    public boolean star(Integer aid, HttpSession session) {
        System.out.println(aid);
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        Map<String, Object> map = new HashMap<>();
        map.put("aid",aid);
        map.put("uid",user.getId());
        List<Star> stars = starMapper.selectByMap(map);
        if(stars==null||stars.size()==0)//如果表中没有
        {
            Star star = new Star(0, aid, user.getId(), 1, 0);
            starMapper.insert(star);
            return true;
        }else {//如果表中有
            Star star = stars.get(0);
            if(star.getStar()==0)//如果没有收藏
            {
                star.setStar(1);
                starMapper.updateById(star);
                return true;//响应蓝色图标
            }else
            {
                star.setStar(0);
                starMapper.updateById(star);
                return false;//响应普通图标
            }
        }
    }
    @RequestMapping("/agree")
    public boolean agree(Integer aid, HttpSession session) {
        System.out.println(aid);
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        Map<String, Object> map = new HashMap<>();
        map.put("aid",aid);

        map.put("uid",user.getId());
        List<Star> stars = starMapper.selectByMap(map);
        if(stars==null||stars.size()==0)//如果表中没有
        {
            Star star = new Star(0, aid, user.getId(), 0, 1);
            starMapper.insert(star);
            return true;
        }else {//如果表中有
            Star star = stars.get(0);
            Article article = articleMapper.selectById(aid);

            if(star.getAgree()==0)//点赞
            {
                article.setAgree(article.getAgree()+1);
                articleMapper.updateById(article);
                star.setAgree(1);
                starMapper.updateById(star);
                return true;//响应蓝色图标
            }else//取消点赞
            {
                article.setAgree(article.getAgree()-1);
                articleMapper.updateById(article);
                star.setAgree(0);
                starMapper.updateById(star);
                return false;//响应普通图标
            }
        }
    }


    @RequestMapping("/attention")
    public boolean attention(Integer uid, HttpSession session) {
        User user = (User) session.getAttribute("user");

        Map<String, Object> map = new HashMap<>();
        map.put("masteruid",user.getId());
        List<Attention> attentions = attentionMapper.selectByMap(map);
        if(attentions==null||attentions.size()==0)//如果表中没有
        {
            Attention attention = new Attention();
            attention.setId(0);
            attention.setMasteruid(user.getId());
            attention.setDoneuid(uid);
            attentionMapper.insert(attention);
            return true;
        }else {//如果表中有
            Attention attention = attentions.get(0);
            attentionMapper.deleteById(attention.getId());
            return false;
        }
    }

}
